package cn.com.ccxe.input;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.core.entity.Website;

public class TextReader implements Reader {

	private InputStream context;
	
	public TextReader() {
		ClassLoader loader = this.getClass().getClassLoader();
		context = loader.getResourceAsStream("keyword_data");
	}
	
	public List<Keyword> readKeyword() {
		if(context == null ) {
			throw new NullPointerException("资源不存在。");
		}
		List<Keyword> keywords = new ArrayList<Keyword>();
		try {
			List<String> list = IOUtils.readLines(context,"utf-8");
			for (String str : list) {
				if(StringUtils.isEmpty(str)) {
					continue;
				}
				str = str.replace(" ", "");
				str = str.replace("*", "");
				Keyword keyword = new Keyword();
				keyword.setText(str);
				keywords.add(keyword);
			}
			return keywords;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

	public List<MonitorType> readMonitorType() {
		//TODO 自动生成的方法。
		return null;
	}

	public List<Website> readWebsite() {
		//TODO 自动生成的方法
		return null;
	}

	public static void main(String[] args) {
		TextReader reader = new TextReader();
		List<Keyword> keywords = reader.readKeyword();
		for (Keyword keyword : keywords) {
			System.out.println(keyword);
		}
	}
	
}
